<?php
namespace app\common\model;
class GoodsCard extends Base
{
    protected $type = [
        'use_time'  =>  'timestamp',
    ];
    protected function initialize()
    {
        parent::initialize();
    }

    public static function getStatusText ($status) {
        if($status==0) {
            return '未使用';
        }elseif($status==1) {
            return '已使用';
        }elseif($status==2) {
            return '已废弃';
        }
    }

    public static function addCard ($inputFileName) {
        //$inputFileName = ROOT_PATH.'cert/1.xlsx';
        /** Load $inputFileName to a Spreadsheet Object  **/
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
        $sheet = $spreadsheet->getActiveSheet();
        $res = array();
        foreach ($sheet->getRowIterator(2) as $row) {
            $tmp = array();
            foreach ($row->getCellIterator() as $cell) {
                $tmp[] = $cell->getFormattedValue();
            }
            //$res[$row->getRowIndex()] = $tmp;
            $log = [
                'number' => $tmp[0],
                'password' => $tmp[1],
                'name' => $tmp[2],
                'sale_uid' => $tmp[3],
                'score' => $tmp[4],
            ];
            $old = self::getOne(['number'=>$log['number']]);
            if($old) {
                self::where(['number'=>$log['number']])->update($log);
            }else{
                self::create($log);
            }
        }
    }
}